package com.bexon.blog;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.Html;
import android.text.method.LinkMovementMethod;
import android.text.util.Linkify;
import android.widget.TextView;

import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
    String url = "http://syzyedu.com/content.aspx?pid=655&id=4358&depid=7";
    String title, subtitle, time, admain, text;
    TextView textView, textView2, textView3, textView4;
    Toolbar toolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = (TextView) findViewById(R.id.tv);
        textView2 = (TextView) findViewById(R.id.tv2);
        textView3 = (TextView) findViewById(R.id.tv3);
        textView4 = (TextView) findViewById(R.id.tv4);
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        new Thread(runnable).start();
    }

    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            Connection conn = Jsoup.connect(url);
            conn.header("User-Agent", "Mozilla/5.0 (Linux; Android 7.1.1; Nexus 5 Build/NMF26O) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.91 Mobile Safari/537.36");
            try {
                Document doc = conn.get();

                title = doc.title();

                Elements body = doc.select("body");
                Elements form = body.select("form#form1");
                Elements div_news = form.select("div.news");
                Elements div_main = div_news.select("div.main");
                Elements div_contw = div_main.select("div.contw");
                Elements div_cont = div_contw.select("div.cont");

                Elements div_2 = div_cont.select("div.t2");
                Elements span_pubtime_baidu = div_2.select("span#pubtime_baidu");
                Elements span_editor_baidu = div_2.select("span#editor_baidu");

                Elements div_t = div_cont.select("div.t");
                Elements p_MsoNormal = div_cont.select("p.MsoNormal");

                Elements span = p_MsoNormal.select("span");

                subtitle = div_t.text();
                time = span_pubtime_baidu.text();
                admain = span_editor_baidu.text();

                text = span.text();

            } catch (IOException e) {
                e.printStackTrace();
            }
            handler.sendEmptyMessage(0);
        }
    };

    @SuppressLint("HandlerLeak")
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            // 当收到消息时就会执行这个方法
            toolbar.setTitle(title);
            textView.setText(subtitle);
            textView2.setText(time + "    " + admain);
            textView3.setText("     " + text);
            textView4.setText("\n\n源地址：" + url);
            textView4.setAutoLinkMask(Linkify.ALL);
            textView4.setMovementMethod(LinkMovementMethod.getInstance());
        }

    };
}
